/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.pub.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.pub.entity.PubDonate;
import com.thinkgem.jeesite.modules.pub.service.PubDonateService;
import com.thinkgem.jeesite.modules.pub.service.PubHelpService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-18
 */
@Controller
@RequestMapping(value = "${adminPath}/pub/pubDonate")
public class PubDonateController extends BaseController {

	@Autowired
	private PubDonateService pubDonateService;
	
	@Autowired
	private PubHelpService pubHelpService;
	
	@ModelAttribute
	public PubDonate get(@RequestParam(required=false) String id) {
		PubDonate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pubDonateService.get(id);
		}
		if (entity == null){
			entity = new PubDonate();
		}
		return entity;
	}
	
	@RequiresPermissions("pub:pubDonate:view")
	@RequestMapping(value = {"list", ""})
	public String list(PubDonate pubDonate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PubDonate> page = pubDonateService.findPage(new Page<PubDonate>(request, response), pubDonate); 
		model.addAttribute("page", page);
		return "modules/pub/pubDonateList";
	}

	@RequiresPermissions("pub:pubDonate:view")
	@RequestMapping(value = "form")
	public String form(PubDonate pubDonate, Model model) {
		model.addAttribute("pubDonate", pubDonate);
		return "modules/pub/pubDonateForm";
	}

	@RequiresPermissions("pub:pubDonate:edit")
	@RequestMapping(value = "save")
	public String save(PubDonate pubDonate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pubDonate)){
			return form(pubDonate, model);
		}
		pubDonateService.save(pubDonate);
		addMessage(redirectAttributes, "保存捐助管理成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubDonate/?repage";
	}
	
	@RequiresPermissions("pub:pubDonate:edit")
	@RequestMapping(value = "delete")
	public String delete(PubDonate pubDonate, RedirectAttributes redirectAttributes) {
		pubDonateService.delete(pubDonate);
		addMessage(redirectAttributes, "删除捐助管理成功");
		return "redirect:"+Global.getAdminPath()+"/pub/pubDonate/?repage";
	}

	
	@RequestMapping(value = "wxsave")
	public String wxsave(HttpServletRequest request, HttpServletResponse response,String username,String userid,String articleid,String amt,String articlename) {
		String id = UUID.randomUUID().toString().replace("-", "");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
		String donatetime = df.format(new Date());
		int result = pubDonateService.wxsave(id,username,userid,articleid,amt,articlename,donatetime);
		if(result>0) {
			pubHelpService.updateamt(userid,articleid);
		}
		return renderString(response, result);
	}
	
	@RequestMapping(value = "wxlist")
	public String wxlist(HttpServletRequest request, HttpServletResponse response,String userid) {
		
		List<Object[]> result = pubDonateService.wxlist(userid);
		
		return renderString(response, result);
	}
}